<script>
export default {
  name:"productCategory",
  data() {
    let checkLoginName =(rule,value,callback)=>{
      if(this.form.id){
        return callback();
      }
      this.$axios.post("/zzh-product-category/findeByOrgNo?name="+this.form.name).then(res=>res.data).then(res=>{
        if(res.resultCode!=200){
          callback()
        }else{
          callback(new Error('类别已经存在'));
        }
      })
    };
    return {
      TreeData:[],
      selectedNode:[],
      selectedId: "",
      selectedValue: null,
      defaultProps: {
        valueL:'id',
        children: 'children',
        label: 'label'
      },
      dialogVisible:false,
      loading: true,
      form:{
        id:'',
        name:'',
        parentId:'',
        sort:'',
        remark:'',
        status:''
      },
      rules: {
        name: [
          { required: true, message: '请输入机构编号', trigger: 'blur' },
          {validator:checkLoginName,trigger: 'blur'}
        ],
      }
    };
  },
  methods:{
    //查询所有商品类别分页
    loadGet(){
      this.loading = true;
      this.$axios.get('/zzh-product-category/list').then(res=>res.data).then(res=>{
        this.loading = false;
        if(res.resultCode==200){
          this.TreeData=res.data
        }else {
          alert('获取数据失败')
        }
      })
    },
    //点击添加按钮，弹出添加对话框
    add(){
      this.dialogVisible=true
      this.$nextTick(()=>{
        this.resetForm()
      })
    },
    //进行添加操作
    doAdd(){
      this.$axios.post('/zzh-product-category/add',this.form).then(res=>res.data).then(res=>{
        if(res.resultCode==200){
          this.dialogVisible=false
          this.resetForm()
          this.$message({
            showClose: true,
            message: '新增数据成功',
            type: 'success'
          });
          this.loadGet()
        }else {
          this.$message({
            showClose: true,
            message: '新增数据失败',
            type: 'error'
          });
        }
      })
    },
    //进行更新操作
    doUpdate(){
      this.$axios.put('/zzh-product-category/update',
          {
            id:this.form.id,
            name:this.form.name,
            parentId:this.form.parentId,
            sort:this.form.sort,
            remark:this.form.remark
          }
      ).then(res=>res.data).then(res=>{
        if(res.resultCode==200){
          this.resetForm()
          this.$message({
            message: '更新数据成功',
            type: 'success'
          });
          this.loadGet()
        }else {
          this.$message({
            showClose: true,
            message: '更新数据失败',
            type: 'error'
          });
        }
      })
    },
    //进行删除操作
    del(){
      if(this.selectedId){
        this.$axios.delete('/zzh-product-category/delete?id='+this.selectedId).then(res=>res.data).then(res=>{
          if(res.resultCode==200){
            this.$message({
              showClose: true,
              message: '删除成功',
              type: 'success'
            });
            this.resetForm()
            this.loadGet()
          }else {
            this.$message({
              showClose: true,
              message: '删除失败',
              type: 'error'
            });
          }
        })
      }
    },
    //重置窗体
    resetForm() {
      this.$refs.form.resetFields();
      this.form.name='';
      this.selectedId='';
      this.selectedValue='';
      this.selectedNode=[];
    },
    //单击查询机构和父机构
    handleNodeClick(data){
      this.selectedId=data.id
      //单击查询机构
      this.$axios.post("/zzh-product-category/findeById?id="+this.selectedId).then(res=>res.data).then(res=>{
        console.log(res.data)
        if(res.resultCode==200){
          this.$nextTick(()=>{
            this.form.id=res.data.id
            this.form.name=res.data.name
            this.form.sort=res.data.sort
            this.form.remark=res.data.remark
            //查询父机构
            if(res.data.parentId){
              this.$axios.post("/zzh-product-category/findeById?id="+res.data.parentId).then(res=>res.data).then(res=>{
                this.selectedValue = res.data.name
              })
            }else {
              this.selectedValue = ""
            }
          })
        }
      })
    },
    //数据筛选
    handleAddClick(data){
      this.selectedNode = [data]; // 将选中的节点数据赋值给selectedNode
      this.selectedValue = data.label; // 将选中的节点值赋值给selectedValue
      this.form.parentId=data.id
    }
  },
  beforeMount() {
    this.loadGet();
  },
  components:{
  }
}
</script>

<template>
  <div style="background-color: #E4E7ED">

    <el-row :gutter="24">
      <!--    按钮-->
      <el-col :span="12"><div class="grid-content" style="background-color: white;padding: 10px">
        <div>
          <el-button type="primary" style="margin-left: 5px" @click="add">添加属性</el-button>
          <el-button type="success" @click="del">删除属性</el-button>
          <el-button type="primary" style="margin-left: 5px" @click="loadGet">刷新</el-button>
        </div>
        <div>
          <el-tree
              :data="TreeData"
              :props="defaultProps"
              default-expand-all
              @node-click="handleNodeClick">
          </el-tree>
        </div>
      </div></el-col>
      <!--    树形目录-->
      <el-col :span="12"><div class="grid-content" style="background-color: white;text-align: center">
        <el-form ref="form" :model="form" :rules="rules" label-width="100px" v-loading="loading">
          <el-form-item v-if="false" label="编号" prop="id" >
            <el-input v-model="form.id" placeholder="请输入编号"></el-input>
          </el-form-item>
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入类别名称"></el-input>
          </el-form-item>
          <el-form-item label="上级目录" prop="parentId">
            <el-select v-model="selectedValue" placeholder="请选择上级目录" style="width: 100%">
              <el-option
                  v-for="item in TreeData" :key="item.id" :value="item.label" :label="item.label"
                  v-show="false"></el-option>
              <el-tree
                  v-model="selectedNode"
                  :data="TreeData"
                  :props="defaultProps"
                  default-expand-all
                  @node-click="handleAddClick"
              ></el-tree>
            </el-select>
          </el-form-item>
          <el-form-item label="排序" placeholder="请输入显示顺序" prop="sort">
            <el-input v-model="form.sort"></el-input>
          </el-form-item>
          <el-form-item label="备注" placeholder="请输入备注" prop="remark">
            <el-input v-model="form.remark"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
        <el-button @click="resetForm">重置</el-button>
        <el-button type="primary" @click="doUpdate">更新</el-button>
      </span>
      </div></el-col>
    </el-row>

    <!--    提示框-->
    <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="40%">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" size="mini">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入类别名称"></el-input>
        </el-form-item>
        <el-form-item label="父机构" prop="parentId">
          <el-select v-model="selectedValue" placeholder="请选择上级目录" style="width: 100%">
            <el-option
                v-for="item in TreeData" :key="item.id" :value="item.label" :label="item.label"
                v-show="false"></el-option>
            <el-tree
                v-model="selectedNode"
                :data="TreeData"
                :props="defaultProps"
                default-expand-all
                @node-click="handleAddClick"
            ></el-tree>
          </el-select>
        </el-form-item>
        <el-form-item label="机构显示顺序" placeholder="请输入显示顺序" prop="sort">
          <el-input v-model="form.sort"></el-input>
        </el-form-item>
        <el-form-item label="备注" placeholder="请输入备注" prop="remark">
          <el-input v-model="form.remark"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="doAdd">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<style scoped>

</style>